package mysql

import (
	"video-up-manager/videorpc/videorpc"

	"gorm.io/gorm"
)

type Collection struct {
	gorm.Model
	UserId        int64  `gorm:"type:int(11);not null;comment:'用户ID'"`
	VideoId       int64  `gorm:"type:int(11);not null;comment:'视频ID'"`
	CollectTime   string `gorm:"type:varchar(50);not ull;comment:'收藏时间'"`
	CollectStatus string `gorm:"type:enum('1','0');default:'0';comment:'收藏状态'"`
}

func (c *Collection) CollectionAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Collection) CollectionList(db *gorm.DB, in *videorpc.CollectionListRequest, lists []*videorpc.CollectionList) ([]*videorpc.CollectionList, error) {
	/*
		SELECT
		  collections.id as collection_id,users.nick_name,videos.video_title,collections.collect_time,collections.collect_status
		FROM
		  collections
		  LEFT JOIN videos ON videos.id= collections.video_id
		  LEFT JOIN users ON users.id = collections.user_id
		  ORDER BY collections.collect_time DESC
	*/
	err := db.Debug().Model(c).
		Select("collections.id as collection_id,users.nick_name,videos.video_title,collections.collect_time,collections.collect_status").
		Joins("LEFT JOIN videos ON videos.id= collections.video_id").
		Joins("LEFT JOIN users ON users.id = collections.user_id").
		Order("collections.collect_time").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&lists).Error
	return lists, err
}

func (c *Collection) FindCollectionById(db *gorm.DB, id int64, id2 int64) error {
	return db.Debug().Where("users.id =? and video.id =?", id, id2).First(c).Error
}

func (c *Collection) CollectionDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id =? ", id).First(c).Error
}
